﻿package net.pezzato.genetree.graphics{
	import fl.controls.ProgressBar;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import net.pezzato.graphics.Photo;
	import net.pezzato.graphics.Widget;
	import net.pezzato.genetree.info.GeneNode;
	import net.pezzato.genetree.graphics.Link;
	import net.pezzato.genetree.graphics.Sky;
	import net.pezzato.genetree.graphics.Container;
	import net.pezzato.genetree.graphics.InfoWidget;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import net.pezzato.graphics.FullStagePhoto;
	public class FullNodeWidget extends NodeWidget {
		private var node_:GeneNode;
		private var mLink_:Link;
		private var fLink_:Link;
		public function FullNodeWidget(wp:Widget, node:GeneNode) {
			node_ = node;
			setupLinks();
			super(wp);
			if (node_ == null) {
				throw Error("invalidNode");
			}
			drawTitle();
			addInfo();
			addPhoto();
			if (node.sex == GeneNode.MALE) {
				colorMatrix_ = MALE_COLOR_MATRIX;
			} else {
				colorMatrix_ = FEMALE_COLOR_MATRIX;
			}
			filters = [colorMatrix_];
		}
		public function get node():GeneNode {
			return node_;
		}
		public function drawLink(myl:Link, thl:Link):void {
			if (parent is Container) {
				(parent as Container).drawLink(myl, thl);
			}
		}
		public function addParents():void {
			if (this.parent is Widget) {
				if (this.parent is Container) {
					var cont:Container = (this.parent as Container);
					if (node_ is GeneNode) {
						if (node_.isReady) {
							if (mNw_ == null) {
								if (node_.knowsParents()) {
									trace(node_.id + " knows parents");
									mNw_ = cont.addNode(node_.m, this);
									mNw_.headLink.linkTo(mLink_);
								} else {
									trace(node_.id + " does not know parents");
									/* TODO:
									 * metti un EmptyNodeWidget
									 * che puo' essere editato per inserire 
									 * un nuovo nodo nell'albero
									 */
								}
							}
							if (fNw_ == null) {
								if (node_.knowsParents()) {
									fNw_ = cont.addNode(node_.f, this);
									fNw_.headLink.linkTo(fLink_);
								} else {
									trace(node_.id + " does not know parents");
									/* TODO:
									 * metti un EmptyNodeWidget
									 * che puo' essere editato per inserire 
									 * un nuovo nodo nell'albero
									 */
								}
							}
						}
					}
				}
			}
		}
		override protected function drawTitle():void {
			if (node_.info != null) {
				if (node_.info[nodeTitle_] != undefined) {
					titleText_ = node_.info[nodeTitle_];
				} else {
					titleText_ = node_.id;
				}
			} else {
				trace("node_.info == null");
				titleText_ = node_.id;
			}
			if (titleText_ == null) {
				titleText_ = "";
			}
			super.drawTitle();
		}
		private function addPhoto():void {
			Photo.adaptNormalSizeToStage(stage);
			if (node_.photo != null) {
				if (node_.photo.@thumbnail != undefined) {
					photo_ = new Photo(node_.photo.@thumbnail, Photo.THUMBNAIL);
				} else {
					if (node_.photo.@src != undefined) {
						photo_ = new Photo(node_.photo.@src, Photo.THUMBNAIL);
					} else {
						throw new Error("errorPhotoNotFound");
					}
				}
			}
			if (photo_ is Photo) {
				photo_.x = EXPA_RECT.width - Photo.T_SIZE - 13;
				photo_.y = EXPA_RECT.height - Photo.T_SIZE - 13;
				photo_.useHandCursor = true;
				photo_.addEventListener(MouseEvent.CLICK, showFullPhoto);
			}
		}
		private function addInfo():void {
			infoWidget_ = new InfoWidget(node_.info, EXPA_RECT.width - 26, EXPA_RECT.height - 66);
			infoWidget_.x = 13;
			infoWidget_.y = 50;
			infoWidget_.mouseEnabled = false;
		}
		private function setupLinks():void {
			headLink_ = new Link(this, 0);
			fLink_ = new Link(this, 2);
			mLink_ = new Link(this, 1);
		}
		private function showFullPhoto(event:MouseEvent = null) {
			trace("showFullPhoto");
			if (node_.photo != null) {
				if (node_.photo.@src != undefined) {
					var fsPhoto:FullStagePhoto = new FullStagePhoto(node_.photo.@src, this);
					var pb:ProgressBar = fsPhoto.progressBar;
					pb.setSize(photo_.width - 5, 10);
					pb.x = photo_.x + 1;
					pb.y = photo_.y + photo_.height - 14;
					addChild(pb);
				} else {
					throw new Error("errorPhotoNotFound");
				}
			}
		}
	}
}